package edu.hitsz.application;

import GUI.ChoicePanel;
import GUI.ScorePanel;
import edu.hitsz.application.game.AbstractGame;
import edu.hitsz.application.game.EasyGame;
import edu.hitsz.application.game.HardGame;
import edu.hitsz.application.game.NormalGame;
import edu.hitsz.data.ScoreObj;

import javax.swing.*;
import java.awt.*;
import java.util.Objects;

/**
 * 程序入口
 *
 * @author hitsz
 */
public class Main {

    public static final int WINDOW_WIDTH = 512;
    public static final int WINDOW_HEIGHT = 768;
    public static boolean musicFlag;
    public static MusicThread bgmThread;
    public static final Object LOCK = new Object();
    public static GameMode gameMode;

    public enum GameMode {
        /**
         * 三种游戏难度
         */
        EASY, NORMAL, HARD
    }

    public static void main(String[] args) {

        System.out.println("Hello Aircraft War");


        // 获得屏幕的分辨率，初始化 Frame
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        JFrame frame = new JFrame("Aircraft War");
        frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        frame.setResizable(false);
        // 设置窗口的大小和位置,居中放置
        frame.setBounds(((int) screenSize.getWidth() - WINDOW_WIDTH) / 2, 0,
                WINDOW_WIDTH, WINDOW_HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 初始选择界面
        frame.setContentPane(new ChoicePanel().getPanel());
        frame.setVisible(true);

        synchronized (LOCK) {
            try {
                LOCK.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // 开启bgm
        if (musicFlag) {
            bgmThread = new MusicThread("bgm.wav", true);
            bgmThread.start();
        }
        // 开始游戏
        AbstractGame game;
        if (gameMode == GameMode.EASY) {
            game = new EasyGame();
        } else if (gameMode == GameMode.NORMAL) {
            game = new NormalGame();
        } else {
            game = new HardGame();
        }

        frame.getContentPane().removeAll();
        frame.setContentPane(game);
        frame.setVisible(true);
        game.action();

        synchronized (LOCK) {
            try {
                LOCK.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 游戏结束, 结束bgm
        if (musicFlag) {
            bgmThread.terminate();
        }

        // 得分面板
        frame.getContentPane().removeAll();
        ScorePanel scorePanel = new ScorePanel();
        frame.setContentPane(scorePanel.getPanel());
        frame.setVisible(true);

        int score = game.getScore();
        String userName = JOptionPane.showInputDialog("游戏结束, 你的得分为 " + score + ",\n请输入名字记录得分:");
        // 点取消则不记录
        if (userName != null) {
            if (Objects.equals(userName, "")) {
                userName = "无名氏";
            }
            scorePanel.addRecord(new ScoreObj(userName, score));
        }
    }

}
